//package com.shuyun.cloud.gateway.filter;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.cloud.gateway.filter.GatewayFilter;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage;
//import org.springframework.cloud.gateway.support.BodyInserterContext;
//import org.springframework.cloud.gateway.support.DefaultServerRequest;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
//import org.springframework.web.reactive.function.BodyInserter;
//import org.springframework.web.reactive.function.BodyInserters;
//import org.springframework.web.reactive.function.server.ServerRequest;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
///**
// * @description: https://www.cnblogs.com/miaoying/p/12531056.html
// * @author: shuai.chen@shuyun.com
// * @date: 2021/12/11 3:14 下午
// **/
//@Slf4j
//public class RequestBodyOperationFilter implements GatewayFilter, Ordered {
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//        if (request.getMethod() != HttpMethod.POST) {
//            return chain.filter(exchange);
//        }
//        return operationExchange(exchange, chain);
//    }
//
//    private Mono<Void> operationExchange(ServerWebExchange exchange, GatewayFilterChain chain) {
//        // mediaType
//        MediaType mediaType = exchange.getRequest().getHeaders().getContentType();
//        log.info("content-type: {}", mediaType.getType());
//        // read & modify body
//        ServerRequest serverRequest = new DefaultServerRequest(exchange);
//        Mono<String> modifiedBody = serverRequest.bodyToMono(String.class)
//                .flatMap(body -> {
//                    // 修改请求体是json格式的
//                    if (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
//                        log.info("=======修改请求体 start =======");
//                        // 对原先的body进行修改操作
//                        log.info("修改前body: {}", body);
//                        String newBody = "{\"id\": 999, \"name\":\"tom\"}";
//                        log.info("修改后body: {}", newBody);
//                        log.info("=======修改请求体 end =======");
//                        return Mono.just(newBody);
//                    }
//                    if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType) || (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType))) {
//                    }
//                    return Mono.empty();
//                });
//        BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, String.class);
//        HttpHeaders headers = new HttpHeaders();
//        headers.putAll(exchange.getRequest().getHeaders());
//        headers.remove(HttpHeaders.CONTENT_LENGTH);
//        CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange, headers);
//        // Mono.then
//        // Mono.defer
//        // Mono.just
//        return bodyInserter.insert(outputMessage, new BodyInserterContext())
//                .then(Mono.defer(() -> {
//                    ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator(exchange.getRequest()) {
//                        @Override
//                        public HttpHeaders getHeaders() {
//                            long contentLength = headers.getContentLength();
//                            HttpHeaders httpHeaders = new HttpHeaders();
//                            httpHeaders.putAll(super.getHeaders());
//                            if (contentLength > 0) {
//                                httpHeaders.setContentLength(contentLength);
//                            } else {
//                                httpHeaders.set(HttpHeaders.TRANSFER_ENCODING, "chunked");
//                            }
//                            return httpHeaders;
//                        }
//
//                        @Override
//                        public Flux<DataBuffer> getBody() {
//                            return outputMessage.getBody();
//                        }
//                    };
//                    return chain.filter(exchange.mutate().request(decorator).build());
//                }));
//    }
//
//    @Override
//    public int getOrder() {
//        return -1;
//    }
//}
